package com.shq.ex1.bus.activity;

import com.shq.ex1.bus.util.UtilMethod;
import com.shq.ex1.R;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.CursorAdapter;
import android.widget.TextView;

public class XianActivity extends Activity implements TextWatcher{
	
	private AutoCompleteTextView xianlu = null;
	
	private Button xianBtn = null;
	private String xianluStr = null;
	private SQLiteDatabase db = null;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.xian);
		xianlu = (AutoCompleteTextView) findViewById(R.id.xianlu);
		
		xianBtn = (Button) findViewById(R.id.xianBtn);
		Intent intent = getIntent();
		final String databasePath = intent.getStringExtra("databasePath");
		db = SQLiteDatabase.openOrCreateDatabase(databasePath, null);
		
		xianlu.addTextChangedListener(this);
		xianBtn.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				if (xianlu != null && xianlu.getText()!= null) {
					xianluStr = xianlu.getText().toString();
				}
				String xianInfoStr = UtilMethod.getXianInfo(databasePath, xianluStr );
				Intent intent = new Intent();
				intent.putExtra("xianInfoStr", xianInfoStr);
				intent.putExtra("xianlu", xianluStr);
				intent.putExtra("databasePath", databasePath);
				intent.setClass(XianActivity.this, XianResultActivity.class);
				XianActivity.this.startActivity(intent);
			}
		});
		
	}
	@Override
	public void afterTextChanged(Editable arg0) {
		Cursor cursor = db.rawQuery("select busw as _id from cnbusw where busw like ?", new String[]{"%" + arg0.toString() +"%"});
		BusAdapter adapter = new BusAdapter(XianActivity.this, cursor, true);
		xianlu.setAdapter(adapter);		
	}

	@Override
	public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
			int arg3) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onTextChanged(CharSequence arg0, int arg1, int arg2, int arg3) {
		// TODO Auto-generated method stub

	}
	public class BusAdapter extends CursorAdapter{

		private LayoutInflater layoutInflater;
		
		public CharSequence convertToString(Cursor cursor)
		{
			return cursor == null ? "" : cursor.getString(cursor
					.getColumnIndex("_id"));
		}
		
		public BusAdapter(Context context, Cursor c, boolean autoRequery) {
			super(context, c, autoRequery);
			layoutInflater = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		}
		
		private void setView(View view,Cursor cursor){
			TextView text = (TextView) view;
			text.setText(cursor.getString(cursor.getColumnIndex("_id")));
		}

		@Override
		public void bindView(View view, Context context, Cursor cursor) {
			setView(view,cursor);
		}

		@Override
		public View newView(Context context, Cursor cursor, ViewGroup parent) {
			View view = layoutInflater.inflate(R.layout.xian_list, null);
			return view;
		}
	}

}
